﻿/*************************************************************************************
 * 文 件 名:   TestUI_UserListManagerPanel
 * 
 * 描    述:   与 NodeJsServer 服务器端通信功能模块，用户信息管理测试页面
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2022/5/14
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using QFramework;
using UnityEngine.UI;
using System;
using HKTools;

public class TestUI_UserListManagerPanel : MonoBehaviour, IController
{
    public IArchitecture GetArchitecture()
    {
        return HKLoginRegister_Architecture.Interface;
    }

    [Header("用户列表信息父物体："), SerializeField] GridLayoutGroup userListGrid;
    [Header("用户信息与蛇体："), SerializeField] GameObject userItem_Prefab;
    [Header("获取用户列表按钮："), SerializeField] Button getUserList_Btn;

    void Start()
    {
        getUserList_Btn.onClick.AddListener(() =>
        {
            this.GetSystem<IHK_LoginRegisterSystem>().GetUserList(GetUserList_Callback);
        });
    }

    void GetUserList_Callback(string msg)
    {
        msg = "{\"userList\":" + msg + "}";

        UserList userList = JsonUtility.FromJson<UserList>(msg);

        for (int i = 0; i < userListGrid.transform.childCount; i++)
            Destroy(userListGrid.transform.GetChild(i).gameObject);

        for (int i = 0; i < userList.userList.Count; i++)
        {
            // 如果是Admin管理员则不显示
            if (userList.userList[i].accountType == "admin")
                continue;

            GameObject userItem = Instantiate(userItem_Prefab);

            userItem.transform.SetParent(userListGrid.transform);

            string username = userList.userList[i].username;

            userItem.GetComponentInChildren<Text>().text = userList.userList[i].realName;
            userItem.GetComponentInChildren<Button>().onClick.AddListener(() =>
            {
                this.GetSystem<IHK_LoginRegisterSystem>().PostDeleAppointUser(username);
                Destroy(userItem);
            });
        }
    }
}

[Serializable]
public class UserList
{
    public List<User> userList;
}

[Serializable]
public class User
{
    public string username;
    public string password;
    public string realName;
    public string accountType;
}